package com.fishbone.cloud.auth.security.support;

import com.fishbone.cloud.auth.security.model.FishBoneUser;
import com.fishbone.cloud.auth.security.utils.TokenUtil;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName FishBoneJwtTokenEnhancer
 * @Author huyaxi
 * @DATE 2020/3/31 15:02
 */
public class FishBoneJwtTokenEnhancer implements TokenEnhancer {

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        FishBoneUser fishBoneUser = (FishBoneUser) authentication.getPrincipal();
        Map<String, Object> info = new HashMap<>(16);
        info.put(TokenUtil.USER_ID,fishBoneUser.getUserId());
        info.put(TokenUtil.ACCOUNT,fishBoneUser.getAccount());
        info.put(TokenUtil.TENANT_ID,fishBoneUser.getTenantId());
        info.put(TokenUtil.ROLE_IDS,fishBoneUser.getRoleIds());
        info.put(TokenUtil.STATUS,fishBoneUser.getStatus());
        info.put(TokenUtil.NAME,fishBoneUser.getName());
        info.put(TokenUtil.PHONE,fishBoneUser.getPhone());
        info.put(TokenUtil.LICENSE,TokenUtil.LICENSE_NAME);
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(info);
        return accessToken;
    }
}
